/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Object.Book;
import Object.Order;
import Object.Page;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Object.User;
import javax.servlet.http.HttpSession;

/**
 *
 * @author PhongLK60473
 */
public class UserController extends HttpServlet {

    private String page_index = "index.jsp";
    private String page_login = "login.jsp";
    private String page_register = "register.jsp";
    private String page_search = "search.jsp";
    private String page_viewcart = "viewcart.jsp";
    private String page_vieworders = "vieworders.jsp";
    private String page_admin = "ManagerController";
    //private String page_index="index.jsp";

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            HttpSession sess = request.getSession();
            User oldUser = (User) sess.getAttribute("User");
            if (action == null || action.equals("") || action.equals("Home")) {
                if (oldUser!=null&&oldUser.isAdmin()) {
                    response.sendRedirect(page_admin);
                } else {
                    Page.forward(page_search, request, response);
                }
            } else if (action.equals("Login")) {
                if (User.isLoging(request)) {
                    User user = new User(request);
                    user.login();
                    sess.setAttribute("User", user);

                    if (user.isGuest()) {
                        Page.setMessage("Login failed", request);
                        Page.forward(page_login, request, response);
                    } else if (user.isCustomer()) {
                        Page.setMessage("You login as customer", request);
                        Page.forward(page_search, request, response);
                    } else if (user.isAdmin()) {
                        Page.setMessage("You login as admin", request);
                        response.sendRedirect(page_admin);
                    } else {
                        // what else ?
                    }
                } else {
                    Page.forward(page_login, request, response);
                }
            } else if (action.equals("Register")) {
                if (User.isRegistering(request)) {
                    if (User.Register(request)) {
                        Page.forward(page_login, request, response);
                    } else {
                        Page.forward(page_register, request, response);
                    }
                } else {
                    Page.forward(page_register, request, response);
                }
            } else if (action.equals("Search")) {
                Page.forward(page_search, request, response);
            } else if (action.equals("Logout")) {
                User.Logout(request);
                Page.forward(page_search, request, response);
            } else if (action.equals("Order")) {
                if (Order.Order(request)) {
                    System.out.println("ordered");
                } else {
                    System.out.println("can not order");
                }

                Page.forward(page_search, request, response);
            } else if (action.equals("ViewCart")) {
                Page.forward(page_viewcart, request, response);
            } else if (action.equals("Checkout")) {
                if(Order.Checkout(request)){
                    Page.forward(page_vieworders, request, response);
                }else{
                    Page.forward(page_viewcart, request, response);
                }
                
            } else if (action.equals("ViewOrders")) {
                Page.forward(page_vieworders, request, response);
            } else {
                // what else ?
                response.sendRedirect(page_index);
            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
